/*
 ============================================================================
 Name		: SCOServerEngine.h
 Author	  : William Zhang
 Version	 : 1.0
 Copyright   : all rights reserved.
 Description : CSCOServerEngine declaration
 ============================================================================
 */

#ifndef SCOSERVERENGINE_H
#define SCOSERVERENGINE_H

// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include <bt_sock.h>
#include <es_sock.h>

class CBluetoothSynchronousLink;
class CBluetoothPhysicalLinks;

// CLASS DECLARATION

/**
 *  CSCOServerEngine
 * 
 */
class CSCOServerEngine : public CBase, 
						public MBluetoothSynchronousLinkNotifier,
						public MBluetoothPhysicalLinksNotifier
	{
public:
	// Constructors and destructor

	/**
	 * Destructor.
	 */
	~CSCOServerEngine();

	/**
	 * Two-phased constructor.
	 */
	static CSCOServerEngine* NewL();

	/**
	 * Two-phased constructor.
	 */
	static CSCOServerEngine* NewLC();

private:

	/**
	 * Constructor for performing 1st stage construction
	 */
	CSCOServerEngine();

	/**
	 * EPOC default constructor for performing 2nd stage construction
	 */
	void ConstructL();

public:
	void Send(const TDesC& aMsg);
	
public:
	//derived from MBluetoothSynchronousLinkNotifier
	
	/** Notification that a synchronous link (SCO) has been set up
	
	 If no error is reported, then the synchronous link is ready for use.
	 @note 1) Observe that although the function signature allows it, this function should
	 not be allowed to leave as the error will be ignored.
	 @note 2) The implementation of this function should NOT be used to delete the associated 
	 CBluetoothSynchronousLink object.
	 @param aErr the returned error
	*/
	void HandleSetupConnectionCompleteL(TInt aErr);

	/** Notification that a synchronous link (SCO) has disconnected
	
	 If no error is reported, then the synchronous link has been closed.
	 @note 1) Observe that although the function signature allows it, this function should
	 not be allowed to leave as the error will be ignored.
	 @note 2) The implementation of this function should NOT be used to delete the associated 
	 CBluetoothSynchronousLink object.
	 @param aErr the returned error
	*/
	void HandleDisconnectionCompleteL(TInt aErr);

	/** Notification that a synchronous link (SCO) has been accepted
	
	 If no error is reported, then we have accepted a request for a synchronous link.
	 That synchronous link is ready for use.
	 @note 1) Observe that although the function signature allows it, this function should
	 not be allowed to leave as the error will be ignored.
	 @note 2) The implementation of this function should NOT be used to delete the associated 
	 CBluetoothSynchronousLink object.
	 @param aErr the returned error
	*/
	void HandleAcceptConnectionCompleteL(TInt aErr);

	/** Notification of a send complete event
	
	 If no error is reported, then an attempt to send synchronous (SCO) data 
	 (e.g. voice) over Bluetooth has succeeded.
	 @note 1) Observe that although the function signature allows it, this function should
	 not be allowed to leave as the error will be ignored.
	 @note 2) The implementation of this function should NOT be used to delete the associated 
	 CBluetoothSynchronousLink object.
	 @param aErr the returned error
	*/
	void HandleSendCompleteL(TInt aErr);

	/** Notification of a receive complete event
	
	 If no error is reported, then then we have successfully received
	 a specified quantity of synchronous (SCO) data.
	 @note 1) Observe that although the function signature allows it, this function should
	 not be allowed to leave as the error will be ignored.
	 @note 2) The implementation of this function should NOT be used to delete the associated 
	 CBluetoothSynchronousLink object.
	 @param aErr the returned error
	*/
	void HandleReceiveCompleteL(TInt aErr);
	
	
public:
	//derived from MBluetoothPhysicalLinksNotifier
	
	/** Notification of a requested connection coming up
	
	 If no error is reported, then that connection is ready for use.
	 @note 1) While this function may leave, any errors are ignored.  Implementers are
	 responsible for performing their own cleanup prior to exiting the function.	 
	 @note 2) The implementation of this function should NOT be used to delete the associated 
	 CBluetoothPhysicalLinks object.
	 
	 @param aErr the returned error
	*/
	void HandleCreateConnectionCompleteL(TInt aErr);

	/** Notification of a requested disconnection having taken place
	
	 If no error is reported, then that connection has been closed.
	 @note 1) While this function may leave, any errors are ignored.  Implementers are
	 responsible for performing their own cleanup prior to exiting the function.
	 @note 2) The implementation of this function should NOT be used to delete the associated 
	 CBluetoothPhysicalLinks object.
	 @param aErr the returned error
	*/
	void HandleDisconnectCompleteL(TInt aErr);

	/** Notification that all existing connections have been torn down
	
	 If no error is reported, then there are no Bluetooth connections existing.
	 @note 1) While this function may leave, any errors are ignored.  Implementers are
	 responsible for performing their own cleanup prior to exiting the function.
	 @note 2) The implementation of this function should NOT be used to delete the associated 
	 CBluetoothPhysicalLinks object.
	 @param aErr the returned error
	*/
	void HandleDisconnectAllCompleteL(TInt aErr);
	
private:
	CBluetoothSynchronousLink* iSCOLink;
	CBluetoothPhysicalLinks* iPhyLink;
	RSocketServ iSockServ;
	TBTDevAddr iAddr;
	TBuf8<50> iMsg;
	
	};

#endif // SCOSERVERENGINE_H
